home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / OUI / gadgetlist.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.5 KB  |  192 lines

  1. // $Id: gadgetlist.cc 1.4 1997/09/17 08:16:09 dlorre Exp dlorre $
  2. #define INTUI_V36_NAMES_ONLY
  3.  
  4. #include <exec/types.h>
  5. #include <exec/memory.h>
  6.  
  7. #include <graphics/gfxbase.h>
  8. #include <libraries/gadtools.h>
  9.  
  10. #include <devices/timer.h>
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #include <mydebug.h>
  16.  
  17. #include <proto/exec.h>
  18. #include <proto/dos.h>
  19. #include <proto/graphics.h>
  20. #include <proto/intuition.h>
  21. #include <proto/gadtools.h>
  22.  
  23. #include "screen.h"
  24. #include "window.h"
  25. #include "gadgetlist.h"
  26. #include "gadgets/gadget.h"
  27.  
  28. static struct EasyStruct GadgetListES = {
  29.     sizeof(struct EasyStruct),
  30.     0,
  31.     (UBYTE *)"GadgetList Corrupted",
  32.     (UBYTE *)"A window is in closing process.\n This window use %ld gadgets but %ld are declared.\nSorry, system will crash",
  33.     (UBYTE *)"Ok"
  34. } ;
  35.  
  36.  
  37. // ========================================================================
  38. // ==========================  GADGETLIST CLASS ===========================
  39. // ========================================================================
  40.  
  41. gadgetlist::gadgetlist(window *w,
  42.                       WORD gmax) :  rectangle(0, 0, 0, 0),
  43.                                     count(0),
  44.                                     max(gmax),
  45.                                     win(w),
  46.                                     bfont(NULL),
  47.                                     initok(FALSE),
  48.                                     glist(NULL)
  49. {
  50.  
  51.     if (gtab = new gadget *[max]) {
  52.         if (ng = new NewGadget) {
  53.             if (it = new IntuiText) {
  54.                 gpen = win->ws->drawinfo->dri_Pens[TEXTPEN] ;     // Front pen
  55.                 ng->ng_VisualInfo = win->ws->vi ;
  56.                 setfont((TTextAttr *)win->ws->scr->Font) ;
  57.                 if (bfont) {
  58.                     ng->ng_GadgetText = NULL ;
  59.                     ng->ng_Width = width ;
  60.                     ng->ng_Height = height ;
  61.                     ng->ng_LeftEdge = left ;
  62.                     ng->ng_TopEdge = top ;
  63.  
  64.                     if (gad = (Gadget *)CreateContext(&glist)) {
  65.                         wp = win->win ;
  66.                         initok = TRUE ;
  67.                     }
  68.                 }
  69.  
  70.             }
  71.         }
  72.  
  73.     }
  74. }
  75.  
  76. gadgetlist::~gadgetlist() {
  77.  
  78.     if (max < count) {
  79.         EasyRequest(NULL, &GadgetListES, NULL, count, max) ;
  80.     }
  81.     if (bfont) CloseFont(bfont) ;
  82.     if (initok) FreeGadgets(glist) ;
  83.     if (gtab) {
  84.     int i ;
  85.         for (i=0; i<count; i++) {
  86. // uncomment the following if your window crashes on exit...
  87. //            D(bug("freeing gtab[%ld] %lx\n", i, gtab[i])) ;
  88.             if (gtab[i]) delete gtab[i] ;
  89.         }
  90.         delete gtab ;
  91.     }
  92.     if (ng) delete ng ;
  93.     if (it) delete it ;
  94. }
  95.  
  96. void gadgetlist::processgadget(LONG id, unsigned long classe, unsigned short code)
  97. {
  98.     gtab[id]->action(classe, code) ;
  99. }
  100.  
  101. void gadgetlist::addgadgets()
  102. {
  103.     if (gad) {
  104.         AddGList(wp, glist, -1, -1, NULL);
  105.         RefreshGList(glist, wp, NULL, -1);
  106.         GT_RefreshWindow(wp, NULL) ;
  107.         win->hasgadgets = TRUE ;
  108.     }
  109.     else
  110.         win->initok = FALSE ;
  111. }
  112.  
  113. void gadgetlist::updategadgets()
  114. {
  115. int i ;
  116.     wp = win->win ;
  117.     for (i=0; i<count; i++)
  118.         if (gtab[i])
  119.             gtab[i]->w = wp ;
  120.     GT_RefreshWindow(wp, NULL) ;
  121.     win->hasgadgets = TRUE ;
  122. }
  123.  
  124. void gadgetlist::setfont(TTextAttr *tta)
  125. {
  126.     CopyMem(tta, &PlainAttr, sizeof(TTextAttr)) ;
  127.     BoldAttr = PlainAttr ;
  128.     BoldAttr.tta_Style |= FSF_BOLD ;
  129.     fontheight = PlainAttr.tta_YSize ;
  130.     if (bfont) CloseFont(bfont) ;
  131.     bfont = OpenFont((TextAttr *)&BoldAttr) ;
  132.     setdefault(FALSE) ;
  133. }
  134.  
  135. void gadgetlist::setdefault(BOOL bolden)
  136. {
  137.     it->ITextFont = ng->ng_TextAttr =
  138.             bolden ?(TextAttr *)&BoldAttr: (TextAttr *)&PlainAttr ;
  139. }
  140.  
  141. long gadgetlist::ltext(const char *s)
  142. {
  143.     it->IText = (UBYTE *)s ;
  144.     return IntuiTextLength(it) ;
  145. }
  146.  
  147. long gadgetlist::lmax(const char **ms)
  148. {
  149. long max=0, l ;
  150.  
  151.     while (*ms)
  152.         if ((l = ltext(*ms++)) > max) max = l ;
  153.  
  154.     return max ;
  155. }
  156.  
  157. long gadgetlist::lmax(const char *s, ...)
  158. {
  159. const char **ms = &s ;
  160.     return lmax(ms) ;
  161. }
  162.  
  163.  
  164. void gadgetlist::selectgadget(LONG id, BOOL shifted)
  165. {
  166.     if (id < count) gtab[id]->keystroke(shifted) ;
  167. }
  168.  
  169. void gadgetlist::parsegadgets(USHORT code)
  170. {
  171. int     i ;
  172. BOOL    shifted ;
  173.  
  174.     if (code > 0x20) {
  175.         shifted = BOOL(isupper(code)) ;
  176.         code = tolower(code) ;
  177.     }
  178.     else
  179.         shifted = FALSE ;
  180.  
  181.     for (i=0; i<count; i++) {
  182.         if (gtab[i]->key == code) {
  183.             gtab[i]->keystroke(shifted) ;
  184.         }
  185.     }
  186. }
  187.  
  188. gadget *gadgetlist::getgadget(LONG id)
  189. {
  190.     return gtab[id] ;
  191. }
  192.